package com.chatroom.controller;

import com.chatroom.dto.ChatMessage;
import com.chatroom.dto.UserDto;
import com.chatroom.service.MessageService;
import com.chatroom.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.messaging.simp.SimpMessageHeaderAccessor;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@Controller
public class ChatController {
    
    @Autowired
    private SimpMessagingTemplate messagingTemplate;
    
    @Autowired
    private MessageService messageService;
    
    @Autowired
    private UserService userService;
    
    @MessageMapping("/chat.sendMessage")
    @SendTo("/topic/public")
    public ChatMessage sendMessage(@Payload ChatMessage chatMessage) {
        chatMessage.setTimestamp(LocalDateTime.now());
        messageService.saveMessage(chatMessage);
        return chatMessage;
    }
    
    @MessageMapping("/chat.addUser")
    @SendTo("/topic/public")
    public ChatMessage addUser(@Payload ChatMessage chatMessage, SimpMessageHeaderAccessor headerAccessor) {
        headerAccessor.getSessionAttributes().put("username", chatMessage.getSender());
        headerAccessor.getSessionAttributes().put("nickname", chatMessage.getSenderNickname());
        
        // 发送系统消息
        ChatMessage systemMessage = new ChatMessage();
        systemMessage.setType("JOIN");
        systemMessage.setContent(chatMessage.getSenderNickname() + " 加入了聊天室");
        systemMessage.setTimestamp(LocalDateTime.now());
        systemMessage.setMessageType("SYSTEM");
        
        messageService.saveMessage(systemMessage);
        
        return systemMessage;
    }
    
    @MessageMapping("/chat.privateMessage")
    public void sendPrivateMessage(@Payload ChatMessage chatMessage) {
        chatMessage.setTimestamp(LocalDateTime.now());
        messageService.saveMessage(chatMessage);
        
        // 发送给特定用户
        messagingTemplate.convertAndSendToUser(
                chatMessage.getReceiver(),
                "/queue/private",
                chatMessage
        );
    }
    
    @GetMapping("/api/users/online")
    @ResponseBody
    public List<UserDto> getOnlineUsers() {
        return userService.getOnlineUsers();
    }
    
    @GetMapping("/api/messages/room/{roomId}")
    @ResponseBody
    public Object getRoomMessages(@PathVariable String roomId, 
                                 @RequestParam(defaultValue = "0") int page,
                                 @RequestParam(defaultValue = "50") int size) {
        return messageService.getRoomMessages(roomId, page, size);
    }
    
    @GetMapping("/api/messages/private/{userId1}/{userId2}")
    @ResponseBody
    public Object getPrivateMessages(@PathVariable Long userId1,
                                   @PathVariable Long userId2,
                                   @RequestParam(defaultValue = "0") int page,
                                   @RequestParam(defaultValue = "50") int size) {
        return messageService.getPrivateMessages(userId1, userId2, page, size);
    }
} 